package com.clps.dev.sms.controller.sm;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.clps.dev.sms.common.CommonData;
import com.clps.dev.sms.common.result.ResponseResult;
import com.clps.dev.sms.common.result.ResultState;
import com.clps.dev.sms.common.result.ResultType;
import com.clps.dev.sms.entity.sm.Department;
import com.clps.dev.sms.service.sm.IDepartmentService;
import com.clps.dev.sms.util.factory.ToolFactory;

/**  
* @ClassName: DepartmentController  
* @Description:  DepartmentController  
* @since JDK 1.8 
* @author RuiShan.Yang  
* @date 2018-12-03 22:35:09  
* 
*/
@Controller
@RequestMapping(value = "/dept")
public class DepartmentController {

	@Resource
	private IDepartmentService departmentService;

	// 显示部门信息界面
	@GetMapping(value = "/list")
	public String showDepartment() {
		return "sm/department";
	}
	

	// 显示树形界面
	@GetMapping(value = "/tree2")
	public String showTree() {
		return "sm/tree2";
	}

	// 显示树形界面
	@GetMapping(value = "/tree")
	public String showTree2() {
		return "sm/tree";
	}

	// 显示树形界面
	@GetMapping(value = "/tree3")
	public String showTree3() {
		return "sm/index";
	}

	@GetMapping(value = "/tree4")
	public String showTree4() {
		return "sm/menu1";
	}

	@GetMapping(value = "/tree5")
	public String showTree5() {
		return "sm/test";
	}

	@GetMapping(value = "/tree6")
	public String showTree6() {
		return "sm/test";
	}

	@GetMapping(value = "/tree7")
	public String showTree7() {
		return "sm/test2";
	}

	@GetMapping(value = "/tree8")
	public String showTree8() {
		return "sm/test3";
	}

	@GetMapping(value = "/tree9")
	public String showTree9() {
		return "sm/test4";
	}

	@GetMapping(value = "/tree10")
	public String showTree10() {
		return "sm/test5";
	}

	@GetMapping(value = "/tree11")
	public String showTree11() {
		return "sm/test";
	}


	// 查询全部
	@ResponseBody
	@GetMapping(value = "/dept")
	public ResponseResult<Department> showAllDepartment() {
		return departmentService.queryAllDepartment();
	}

	// 添加部门
	@ResponseBody
	@PostMapping(value = "/dept", produces = { "application/json;charset=UTF-8" })
	public ResponseResult<Department> addDepartment(@RequestBody Department department) {
		department.setDepartmentCreatedTime(ToolFactory.getDateInstance(CommonData.YYYY_MM_DD_HH_MM_SS));
		department.setDepartmentCreatedEmployeeId(3);
		department.setDepartmentUpdatedEmployeeId(2);
		ResponseResult<Department> responseResult = departmentService.addDepartment(department);
		if (ResultType.ADD_SUCCESS.equals(responseResult.getMessage())) {
			responseResult.setState(ResultState.SAVESUCCESS);
		}
		return responseResult;
	}

	// 修改
	@ResponseBody
	@PutMapping(value = "/dept")
	public ResponseResult<Department> editDepartment(@RequestBody Department department) {

		ResponseResult<Department> responseResult = departmentService.editDepartment(department);
		if (ResultType.UPDATE_SUCCESS.equals(responseResult.getMessage())) {
			responseResult.setState(ResultState.UPDATE_SUCCESS);
		} else {
			responseResult.setState(ResultState.UPDATE_FAULT);
		}
		return responseResult;
	}

	// 删除
	@ResponseBody
	@DeleteMapping(value = "/dept/{departmentId}")
	public ResponseResult<Department> deleteDepartment(@PathVariable("departmentId") int departmentId) {
		
		ResponseResult<Department> responseResult = departmentService.deleteDepartmentById(departmentId);
		
		
		
		
		if (ResultType.DELETE_SUCCESS.equals(responseResult.getMessage())) {
			responseResult.setState(ResultState.REMOVESUCCESS);
		} else {
			responseResult.setState(ResultState.REMOVEERROR);
		}
		return responseResult;
	}

	// 根据部门名称模糊查询
	@ResponseBody
	@GetMapping(value = "/deptName/{departmentName}")
	public ResponseResult<Department> showDepartmentByLikeName(@PathVariable("departmentName") String departmentName) {
		ResponseResult<Department> responseResult = null;
		if ("".equals(departmentName)) {
			responseResult = departmentService.queryAllDepartment();
			responseResult.setState(ResultState.QUERYERROR);
		} else {
			responseResult = departmentService.queryDepartmentByLikeName(departmentName);
			responseResult.setState(ResultState.QUERYSUCCESS);
		}
		return responseResult;
	}

	// 根据部门ID查询
	@ResponseBody
	@GetMapping(value = "/deptId/{departmentId}")
	public ResponseResult<Department> showDepartmentById(@PathVariable("departmentId") String department) {
		int departmentId = Integer.parseInt(department);
		ResponseResult<Department> responseResult = null;
		if ("".equals(department)) {
			responseResult = departmentService.queryAllDepartment();
			responseResult.setState(ResultState.QUERYERROR);
			return responseResult;
		} else {
			responseResult = departmentService.queryDepartmentById(departmentId);
			responseResult.setState(ResultState.QUERYSUCCESS);
			return responseResult;
		}
	}

	// 查询唯一性
	@ResponseBody
	@GetMapping(value = "/check/{departmentFieldName}/{departmentFieldVal}")
	public ResponseResult<Department> checkUnique(@PathVariable("departmentFieldName") String departmentFieldName,
			@PathVariable("departmentFieldVal") String departmentFieldVal) {
		System.err.println(departmentFieldName + "------------------------" + departmentFieldVal);

		ResponseResult<Department> responseResult = departmentService.isUniqueByField(departmentFieldName,
				departmentFieldVal);

		System.err.println(responseResult.getMessage() + "+++++++++++++++++++++++++++++++++++++++++++++++=");

		if (ResultType.CONTENT_REPEAT.equals(responseResult.getMessage())) {
			responseResult.setState(ResultState.DUPLICATIONERROR);
		} else {
			responseResult.setState(ResultState.OPERATIONSUCCESS);
		}
		return responseResult;

	}

}
